﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.Build.Player;
using System.IO;
using Main;
using System.Drawing.Printing;
using UnityEditor.VersionControl;

public class HotfixUtilities : Editor
{
    [MenuItem("Tools/XLua/Copy Lua To TxT")]
    private static void CopyLuaToTxT()
    {
        string targetPath = PathConfig.HotfixPath;
        if (Directory.Exists(targetPath))
        {
            string[] files = Directory.GetFiles(targetPath, "*.lua", SearchOption.AllDirectories);
            string DllPath = PathConfig.CodeDllPath;
            if (Directory.Exists(DllPath) == false)
            {
                Directory.CreateDirectory(DllPath);
            }
            else
            {
                DirectoryInfo dir = new DirectoryInfo(DllPath);
                FileInfo[] fileInfos = dir.GetFiles();
                foreach (FileInfo fileInfo in fileInfos)
                {
                    fileInfo.Delete();
                }
            }
            foreach (var file in files)
            {
                FileInfo info = new FileInfo(file);
                string newFile = DllPath + info.Name + ".txt";
                File.Copy(file, newFile, true);
            }
            string[] guids = AssetDatabase.FindAssets(".lua", new string[] { DllPath });
            foreach (var guid in guids)
            {
                // 获取资源的路径
                string path = AssetDatabase.GUIDToAssetPath(guid);

                // 获取资源的AssetImporter
                AssetImporter assetImporter = AssetImporter.GetAtPath(path);

                // 设置AssetBundle名称
                assetImporter.assetBundleName = "code";
            }
            // 刷新项目窗口，以显示更改
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            Debug.Log("Lua转Text完成");
        }
    }
}
